<?php

/*
 * 登陆验证相关类
 */

class LoginAuth {

    /**
     * 检查登陆状态
     */
    public static function checkLogin() {
        global $_SGLOBAL;
        if (Cookie::_isset('sid')) {
            $_SGLOBAL['sid'] = Cookie::_get('sid');
        } else {
            $_SGLOBAL['sid'] = Utils::getSid();
            Cookie::_set('sid', $_SGLOBAL['sid']);
        }
        if (Cookie::_isset('auth')) {
            list($_SGLOBAL['supe_uid'], $_SGLOBAL['supe_password']) = explode("\t", self::decode(Cookie::_get('auth')));
        } else {
            list($_SGLOBAL['supe_uid'], $_SGLOBAL['supe_password']) = array(0, '');
        }

        if ($_SGLOBAL['supe_uid'] && $_SGLOBAL['db']->getCount('member', array('uid' => $_SGLOBAL['supe_uid'], 'password' => $_SGLOBAL['supe_password']))) {
            $_SGLOBAL['member'] = User::getUser($_SGLOBAL['supe_uid']);
            $_SGLOBAL['supe_gid'] = $_SGLOBAL['member']['gid'];
            $_SGLOBAL['supe_user'] = $_SGLOBAL['member']['username'];
        } else {
            $_SGLOBAL['supe_uid'] = 0;
            $_SGLOBAL['supe_password'] = $_SGLOBAL['supe_user'] = '';
        }
    }

    /**
     * 加密字符串
     * @param string $str 要加密的字符串
     */
    public static function encode($str) {
        $str = base64_encode(urlencode($str));
        return $str;
    }

    /**
     * 解密字符串
     * @param string $str 要解密的字符串
     */
    public static function decode($str) {
        $str = urldecode(base64_decode($str));
        return $str;
    }

}

?>
